using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Admin_Admin_Dictionary : Admin, IRequiresSessionState
{
	protected ScriptManager sm;
	protected Literal ltrTitle;
	protected HtmlTable table_item;
	protected TextBox txtSearchTitle;
	protected Button SeachBut;
	protected GridView gvManage;
	protected Literal ltrPagePanel;
	protected Panel pnlManage;
	protected Label lblAddOrEditTitle;
	protected DropDownList ddlTableName;
	protected DropDownList ddlFieldName;
	protected UpdatePanel upAddEdit;
	protected TextBox txtTitle;
	protected TextBox txtFieldText;
	protected TextBox txtFieldValue;
	protected Button btnSave;
	protected Panel pnlAddOrEditInfo;
	private string linkHead = "";
	private string beforeUrl = "Admin_Dictionary.aspx?MenuID=6&MenuName=&#x7CFB;&#x7EDF;&FileName=字典表管理";
	private string actionTab = "";
	private int fieldID = 0;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public string ActionTab
	{
		get
		{
			if (base.Request.QueryString["action"] != null)
			{
				this.actionTab = base.Request.QueryString["action"].Trim();
			}
			else
			{
				this.actionTab = "";
			}
			return this.actionTab;
		}
		set
		{
			this.actionTab = value;
		}
	}
	public int FieldID
	{
		get
		{
			if (base.Request.QueryString["fieldID"] != null)
			{
				this.fieldID = Convert.ToInt32(base.Request.QueryString["fieldID"].Trim());
			}
			return this.fieldID;
		}
		set
		{
			this.fieldID = value;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.admin.CheckSuperAdmin())
			{
				base.WriteLog("Modidy", Language.GetAdminMsg("Log/Access", "0"), "Admin_Dictionary");
				base.WriteRightErrMsg();
			}
			this.ShowPanelDisplay(this.ActionTab);
		}
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (DisplayName != null)
		{
			if (DisplayName == "addinfo")
			{
				this.pnlManage.Visible = false;
				this.pnlAddOrEditInfo.Visible = true;
				this.lblAddOrEditTitle.Text = "添加字典表信息";
				return;
			}
			if (DisplayName == "editinfo")
			{
				this.pnlManage.Visible = false;
				this.pnlAddOrEditInfo.Visible = true;
				this.lblAddOrEditTitle.Text = "修改字典表信息";
				this.InitPage();
				return;
			}
		}
		this.pnlManage.Visible = true;
		this.pnlAddOrEditInfo.Visible = false;
		this.BindDictionary();
	}
	protected void BindDictionary()
	{
		string query = this.GetQuery();
		string queryByOrder = this.GetQueryByOrder();
		int num = this.gvManage.PageIndex;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		string text = " from HL_Dictionary where 1=1 ";
		int pageSize = this.gvManage.PageSize;
		ClientByPage clientByPage = new ClientByPage();
		int listCount = clientByPage.GetListCount("select count(*)" + text + query);
		int startIndex = (num - 1) * pageSize + 1;
		int endIndex = num * pageSize;
		string str = "ROW_NUMBER() OVER (" + queryByOrder + ") as row,* ";
		string strSQL = "select " + str + text + query;
		DataTable listPage = clientByPage.GetListPage(strSQL, "row", startIndex, endIndex);
		this.gvManage.DataSource = listPage;
		this.gvManage.DataBind();
		CurrPageLinks currPageLinks = new CurrPageLinks();
		string url = this.beforeUrl + this.linkHead + "&pageindex";
		this.ltrPagePanel.Text = currPageLinks.CurrPageLink(num, pageSize, listCount, "条记录&nbsp;&nbsp;每页&nbsp;" + pageSize + "&nbsp;条记录", url);
	}
	private string GetQuery()
	{
		string text = "";
		string text2 = Fetch.Get("TitleS");
		if (!string.IsNullOrEmpty(text2))
		{
			this.txtSearchTitle.Text = text2;
		}
		else
		{
			text2 = this.txtSearchTitle.Text;
		}
		if (!string.IsNullOrEmpty(text2))
		{
			text = text + " and Title like '%" + text2 + "%'";
			this.linkHead = this.linkHead + "&TitleS=" + text2;
		}
		return text;
	}
	private string GetQueryByOrder()
	{
		return "order by FieldID,TableName desc";
	}
	protected void gvManage_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
		}
	}
	protected void gvManage_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		this.gvManage.PageIndex = e.NewPageIndex;
		this.BindDictionary();
	}
	protected void Button_Command(object sender, CommandEventArgs e)
	{
		string commandName = e.CommandName;
		string str = this.linkHead + "&pageindex";
		if (commandName == "DelInfo")
		{
			string value = e.CommandArgument.ToString();
			if (!string.IsNullOrEmpty(value))
			{
				HoWave.Data.Model.Dictionary model = HoWave.Data.Bll.Dictionary.instance.GetModel(Convert.ToInt32(value));
				if (model != null)
				{
					bool flag = HoWave.Data.Bll.Dictionary.instance.Delete(Convert.ToInt32(value));
					if (flag)
					{
						this.WriteSuccessMsg("", "删除成功！", this.beforeUrl + str);
					}
					else
					{
						this.WriteErrMsg("", "删除失败！", this.beforeUrl + str);
					}
				}
			}
		}
	}
	protected void SeachBut_Click(object sender, EventArgs e)
	{
		this.BindDictionary();
	}
	private void BindFieldName()
	{
		this.ddlFieldName.Items.Clear();
		if (this.ddlTableName.SelectedValue.Trim() != "")
		{
			string sql = "select name from sys.columns where object_id in (select object_id from  sys.tables where name='" + this.ddlTableName.SelectedValue.Trim() + "')";
			DataTable dataTable = this.conn.GetDataTable(sql);
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				string text = dataTable.Rows[i]["name"].ToString();
				ListItem listItem = new ListItem();
				listItem.Text = text;
				listItem.Value = text;
				this.ddlFieldName.Items.Add(listItem);
			}
		}
	}
	private void BindTableName()
	{
		this.ddlTableName.Items.Clear();
		string sql = "select * from sys.tables order by name asc";
		DataTable dataTable = this.conn.GetDataTable(sql);
		for (int i = 0; i < dataTable.Rows.Count; i++)
		{
			string text = dataTable.Rows[i]["name"].ToString();
			ListItem listItem = new ListItem();
			listItem.Text = text;
			listItem.Value = text;
			this.ddlTableName.Items.Add(listItem);
		}
		this.ddlTableName.Items.Insert(0, new ListItem("——请选择——", ""));
	}
	protected void ddlTableName_SelectedIndexChanged(object sender, EventArgs e)
	{
		this.BindFieldName();
	}
	private void InitPage()
	{
		if (this.FieldID > 0)
		{
			HoWave.Data.Model.Dictionary model = HoWave.Data.Bll.Dictionary.instance.GetModel(this.FieldID);
			if (model != null)
			{
				this.ddlTableName.ddlSelect(model.TableName);
				this.BindFieldName();
				this.ddlFieldName.ddlSelect(model.FieldName);
				this.txtTitle.Text = model.Title;
				this.txtFieldText.Text = model.FieldText;
				this.txtFieldValue.Text = model.FieldValue;
				this.txtFieldValue.Enabled = false;
			}
		}
	}
	private HoWave.Data.Model.Dictionary GetInfoModel()
	{
		HoWave.Data.Model.Dictionary dictionary = new HoWave.Data.Model.Dictionary();
		string tableName = this.ddlTableName.SelectedValue.Trim();
		string fieldName = this.ddlFieldName.SelectedValue.Trim();
		string text = this.txtTitle.Text;
		string text2 = this.txtFieldText.Text;
		string text3 = this.txtFieldValue.Text;
		dictionary.TableName = tableName;
		dictionary.FieldName = fieldName;
		dictionary.Title = text;
		dictionary.FieldText = text2;
		dictionary.FieldValue = text3;
		return dictionary;
	}
	private bool CheckInput()
	{
		bool result = true;
		if (this.ActionTab == "addinfo")
		{
			HoWave.Data.Model.Dictionary dictionary = HoWave.Data.Bll.Dictionary.instance.GetDictionary(this.txtFieldValue.Text.Trim());
			if (dictionary != null)
			{
				MessageBox.Show(this, string.Concat(new string[]
				{
					"字段的实际值不能重复!在",
					dictionary.TableName,
					"表中字段为",
					dictionary.FieldName,
					",名称为",
					dictionary.Title,
					"存在字段的实际值！"
				}));
				result = false;
			}
		}
		return result;
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			HoWave.Data.Model.Dictionary infoModel = this.GetInfoModel();
			string text = this.ActionTab;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (text == "editinfo")
					{
						infoModel.FieldID = this.FieldID;
						bool flag = HoWave.Data.Bll.Dictionary.instance.Update(infoModel);
						if (flag)
						{
							this.WriteSuccessMsg("", "字典表修改成功！", this.beforeUrl);
						}
						else
						{
							this.WriteErrMsg("", "字典表修改失败！", "");
						}
					}
				}
				else
				{
					int num = HoWave.Data.Bll.Dictionary.instance.Add(infoModel);
					if (num > 0)
					{
						this.WriteSuccessMsg("", "字典表添加成功！", this.beforeUrl);
					}
					else
					{
						this.WriteErrMsg("", "字典表添加失败！", "");
					}
				}
			}
		}
	}
}
